<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/18
 * Time: 11:43
 */

namespace App\Services;


use App\Logic\GroupLogic;
use App\Logic\UserGroupLogic;
use App\Model\FriendModel;
use App\Model\GroupModel;
use App\Model\UserGroupModel;
use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Component\TableManager;

class BroadcastService
{
    /**
     * 推送群、群成员哈希给客户端
     */
    public function  appready($param){
        go(function () use($param) {
            $userInfo = $param['userInfo'];
            #计算群哈希
            $groupLogic = new GroupLogic();
            $extra['group_hash'] =  $groupLogic->groupHash($userInfo['id']);
            #计算群成员哈希
            $userGroupLogic = new UserGroupLogic();
            $extra['group_member_hash_list'] = $userGroupLogic->groupHashAll($userInfo['id']);
            $messageArr['nonce'] = $param['nonce'];
            $messageArr['content'] = '';
            $messageArr['action'] = 'appready';
            $messageArr['time'] = HelpService::msectime();
            $messageArr['msg'] = ReponseCodeService::$phrases[ReponseCodeService::CODE_OK];
            $messageArr['code'] = ReponseCodeService::CODE_OK;
            $data['extra'] =  $extra;
            $messageArr['data'] = json_encode($data);

            $pushMessageService = new PushMessageService();
            return   $pushMessageService->push($param['fd'],$messageArr,true);
        });
    }

    /**
     * 用户信息变动
     * showdoc
     * @catalog Websocket/服务端主动推送
     * @title  用户信息变动
     * @description 联系人昵称、头像变动，系统会主动下发相关消息，客户端收到消息请变更相应信息，客户端收到消息无需回传任何消息给服务端
     * @method
     * @url /
     * @return {"code":200,"result":'WOut2yeSh2TFgRrrDCmgXw==',"msg":""}
     * @return_param content  string  内容
     * @return_param action  string  动作，固定是systemMessage
     * @return_param  nonce   string 消息随机字符串，由时间戳+6位随机字符串，用于消息校验
     * @return_param  extra string 额外信息，有需要增加的字段，全部往这边塞
     * @return_param  msgId   string 消息id
     * @return_param  user_id   string 用户id，这里的用户id是好友、联系人的用户id，当id等于自己时候，请更新自己的信息
     * @return_param  app_user_id   string 用户的app_user_id
     * @return_param  nickname   string 用户昵称
     * @return_param  head   string 用户头像
     */
    public function accountChange($userInfo){
        go(function () use($userInfo) {
            $chatService = new ChatService();
            try{
                #给最近联系人广播变动信息
                $friendModel = new FriendModel();
                $list = $friendModel->all(['user_id'=>$userInfo['id']])->column('friend_user_id');
                $extra['user_id'] = $userInfo['id'];
                $extra['nickname'] = $userInfo['nickname'];
                $extra['type'] = $userInfo['type'];
                $extra['app_user_id'] = $userInfo['app_user_id'];
                $sysMessageArr = array(
                    'to_app_user_id'=>$userInfo['app_user_id'],
                    'content'=>'',
                    'type'=>PrivateMsgService::MSG_TYPE_BASE,
                    'extra'=>$extra,
                    'sub_type'=>PrivateMsgService::MSG_SUB_TYPE_PERSONAL,
                    'nonce'=>HelpService::msectime().HelpService::createNonce(6),
                    'app_id'=>$userInfo['app_id'],
                    'msg_id'=>HelpService::msectime().HelpService::createNonce(6)
                );
                if(!empty($list)){
                    //实例化
                    foreach ( $list as $toUserId){
                        $message['to_user_id'] = $toUserId;
                        $chatService ->systemMessage( $sysMessageArr);
                    }
                }
                #给群发消息变动信息
                $groupList = UserGroupModel::create()->all(['user_id'=>$userInfo['id']]);
                if(!empty($groupList)){
                    foreach ($groupList as $userGroup){
                        $group = GroupModel::create()->all(['id'=>$userGroup['group_id']]);
                        $sysMessage = array(
                            'group_id'=>$group['id'],
                            'content'=>'',
                            'app_id'=>$userInfo['app_id'],
                            'type'=>PrivateMsgService::MSG_TYPE_GROUP_INVITE,
                            'nonce'=>HelpService::msectime().HelpService::createNonce(6)
                        );
                        $chatService->groupSysMessage($sysMessage);
                    }
                }
            }catch(\Throwable $t){
                return false;
            }
        });
        return true;
    }
}